import { _decorator, sys } from 'cc';
import ChannelTools from './base/ChannelTools';
import { BytedanceChannel } from './channel/BytedanceChannel';
import { ChannelData } from './ChannelData';
import WebChannel from './channel/WebChannel';
import { WeixinChannel } from './channel/WeixinChannel';
import { KuaishouChannel } from './channel/KuaishouChannel';
const { ccclass } = _decorator;

@ccclass('ChannelManager')
export class ChannelManager extends ChannelTools {
    /**单例模式 */
    private static instance: ChannelManager;
    /**获取单例 */
    public static GetInstance(): ChannelManager {
        return this.instance;
    }

    /**渠道初始化 */
    protected Init() {
        ChannelManager.instance = this;
        switch (sys.platform) {
            case sys.Platform.WECHAT_GAME:
                if (window['platform'] == "kuaishou") {
                    this.channel = new KuaishouChannel("", [""], [], [], [], [], []);
                } else {
                    this.channel = new WeixinChannel("", [""], [], [], [], [], []);
                }
                break;
            case sys.Platform.BYTEDANCE_MINI_GAME:
                this.channel = new BytedanceChannel("", [''], [], [], [], [], []);
                break;
            default:
                this.channel = new WebChannel("", [], [], [], [], [], []);
                break;
        }
        this.OnHide();
    }

    /**
     * Toast提示
     * @param content 提示内容，单行显示，建议不要过长
     */
    public ShowToast(content: string) {
        this.showToast(content);
    }

    /**
     * 显示提示框
     * @param data 提示框数据
     * @param ->title string 提框框标题(选填)，默认为“提 示”
     * @param ->content string 提框框标题(必填)，提示的内容
     * @param ->sure Function 点击确认的回调(选填)
     * @param ->cancel Function 点击取消的回调(选填)
     * @param ->sure_label 确认按键上的文字(选填)，默认为“确 定”
     * @param ->cancel_label string 取消按键上的文字(选填)，默认为“取 消”
     * @param ->ad_icon boolean 是否有广告图标(选填)，默认没有
     */
    public ShowDialog(data: any) {
        this.showDialog(data);
    }

    /**
     * 显示视频广告
     * @param on_successed 成功回调
     * @param on_failed 失败回调
     * @param caller 作用域
     */
    public ShowVideo(on_successed: Function, on_failed: Function, caller: any) {
        if (this.channel) {
            if (ChannelData.isDev) {
                on_successed.call(caller);
            } else if (ChannelData.isHasAd) {
                this.channel.ShowVideo(on_successed, on_failed, null, caller);
            } else {
                this.ShowToast('暂时没有广告！');
            }
        } else {
            this.ShowToast('渠道初始化失败！');
        }
    }

    /**
     * 检测是否有侧边栏
     * @param success 
     * @param fail 
     * @param caller 
     */
    public CheckScene(success: Function, fail: Function, caller: any) {
        if (this.channel) {
            this.channel.CheckScene(success, fail, caller);
        } else {
            this.ShowToast('渠道初始化失败！');
        }
    }

    /**去侧边栏 */
    public NavigateToScene() {
        if (this.channel) {
            this.channel.navigateToScene();
        } else {
            this.ShowToast('渠道初始化失败！');
        }
    }

    public SaveToDesktop() {
        if (this.channel) {
            this.channel.navigateToScene();
        } else {
            this.ShowToast('渠道初始化失败！');
        }
    }

    /**
     * 设置系统剪贴板内容
     * @param data 剪贴板的内容
     * @param success 成功回调
     * @param fail 失败回调
     * @param complete 完成回调
     * @param caller 
     */
    public SetClipboardData(data: string, success: Function, fail: Function, complete: Function, caller: any) {
        if (this.channel) {
            this.channel.SetClipboardData(data, success, fail, complete, caller);
        } else {
            this.ShowToast('渠道初始化失败！');
        }
    }

    public VibrateShort() {
        if (this.channel && ChannelData.vibrate) {
            this.channel.VibrateShort();
        }
    }

    public VibrateLong() {
        if (this.channel && ChannelData.vibrate) {
            this.channel.VibrateLong();
        }
    }

    public OnHide() {
        if (this.channel) {
            this.channel.OnHide((data) => { });
        }
    }

    public GetSystemInfo() {
        if (this.channel) {
            return this.channel.GetSystemInfo();
        }
        return null;
    }
}

